package com.yijiu.trb;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.RegistResponse;
import com.yijiu.utils.DialogUtils;

/**
 * 注册页面
 * 
 * @author TangChao
 */
public class RegistActivity extends BTitleActivity
{
	// 帐户名、真实姓名、证件号、邮箱、推荐人号
	private EditText nameEditText, realNameEditText, emailEditText, oneUpNumEditText;

	// 同意用户协议按钮
	private CheckBox isAgreeCheckBox;

	private String nameStr, emailStr;

	private Button nextRegistButton;

	private String realNameStr, oneUpNumStr, flag;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.activity_regist);
		initWidget();
	}

	/** 初始化控件 */
	@Override
	protected void initWidget()
	{
		super.initWidget();
		flag = getIntent().getStringExtra("flag");
		if ("moneyMan".equals(flag))
		{
			titleView.setText("投资人开户");
		}
		else
		{
			titleView.setText("注册");
		}
		SpannableString sp = new SpannableString("我已阅读并同意《用户协议》");
		TextView agreeTextView = (TextView) findViewById(R.id.tv_regist_one_info);
		sp.setSpan(new CheckAgreeText(""), 8, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		sp.setSpan(new ForegroundColorSpan(Color.BLACK), 8, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		agreeTextView.setText(sp);
		agreeTextView.setMovementMethod(LinkMovementMethod.getInstance());
		nameEditText = (EditText) findViewById(R.id.et_regist_one_name);
		realNameEditText = (EditText) findViewById(R.id.et_regist_one_real_name);
		emailEditText = (EditText) findViewById(R.id.et_regist_one_email);
		oneUpNumEditText = (EditText) findViewById(R.id.et_regist_one_up_num);
		isAgreeCheckBox = (CheckBox) findViewById(R.id.cb_regist_one_isagree);
		nextRegistButton = (Button) findViewById(R.id.btn_regist_one_done);
		if ("moneyMan".equals(getIntent().getStringExtra("flag")))
		{
			oneUpNumEditText.setVisibility(View.GONE);
			oneUpNumStr = CacheData.MEMBER_NO;
		}
		isAgreeCheckBox.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				if (isChecked)
				{
					nextRegistButton.setEnabled(true);
					nextRegistButton.setBackgroundResource(R.drawable.login_background);
				}
				else
				{
					nextRegistButton.setEnabled(false);
					nextRegistButton.setBackgroundResource(R.drawable.btn_login_bg_false);
				}
			}
		});
	}

	public void registOneClick(View v)
	{
		int id = v.getId();
		switch (id)
		{
			case R.id.btn_regist_one_done:
				dataCheckAndRegist();
				break;
			default:
				break;
		}
	}

	/** 取值，请求 */
	private void dataCheckAndRegist()
	{
		nameStr = nameEditText.getText().toString();
		realNameStr = realNameEditText.getText().toString();
		emailStr = emailEditText.getText().toString();
		if (!"moneyMan".equals(getIntent().getStringExtra("flag")))
		{
			oneUpNumStr = oneUpNumEditText.getText().toString();
		}
		if (StringUtils.isNull(nameStr))
		{
			app.showMsg("请输入账户名");
			return;
		}
		if (nameStr.length() < 6 || nameStr.length() > 20)
		{
			app.showMsg("账户名长度应在6~20位之间");
			return;
		}
		if (StringUtils.isNull(realNameStr))
		{
			app.showMsg("请输入真实姓名");
			return;
		}
		if (StringUtils.isNull(emailStr))
		{
			app.showMsg("请输入电子邮箱");
			return;
		}
		if (!StringUtils.isEmail(emailStr))
		{
			app.showMsg("请输入有效的电子邮箱");
			return;
		}
		// 将用户输入的信息拼接成字符串上传给服务器
		StringBuffer sb = new StringBuffer();
		sb.append("service=register&userName=" + nameStr + "&realName=" + realNameStr + "&mail=" + emailStr);
		if (!StringUtils.isNull(oneUpNumStr))
			sb.append("&referees=" + oneUpNumStr);
		String inputStr = sb.toString();
		// AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 封装上传的数据
		BaseAction registAction = new BaseAction(encodedData, encriptData);
		// 注册请求的响应
		RegistResponse registResponse = new RegistResponse();
		Request r = new Request(registAction, registResponse, ActionType.REGIST_CODE);
		netManager.excute(r, this, this);
		showProgress("正在注册，请稍候！");
	}

	/**
	 * 操作成功返回的函数
	 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		int type = r.getType();
		switch (type)
		{
			case ActionType.REGIST_CODE:
				if ("moneyMan".equals(flag))
				{
					app.showMsg("开户成功！");
					initIntent(this, OpenAccountDoneActivity.class);//
					finish();
				}
				else
				{
					app.showMsg("注册成功！");
					RegistResponse registResponse = (RegistResponse) r.getResponse();
					Intent intent = new Intent(this, RegistDoneActivity.class);
					intent.putExtra("nameStr", nameStr);
					intent.putExtra("emailStr", emailStr);
					intent.putExtra("realNameStr", realNameStr);
					intent.putExtra("md5Id", registResponse.field.MD5);
					startActivity(intent);
					finish();
					CacheData.MD5_USER_ID = registResponse.field.MD5;
					CacheData.REAL_NAME = realNameStr;
				}
				break;
		}
	}

	/* 操作失败返回的函数 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.REGIST_CODE:
				app.showMsg(errorMsg);
				break;
		}
	}

	/* 重复提交返回的函数 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}

	class CheckAgreeText extends URLSpan
	{
		public CheckAgreeText(String src)
		{
			super(src);
		}

		@Override
		public void onClick(View widget)
		{
			DialogUtils.showAgreeTxtDialog(RegistActivity.this, R.string.txt4);
		}
	}
}
